<?php
namespace app\admin\controller;
use think\Db;
class Article extends Common
{

    //首页文章展示
    public function index()
    {
        //文章分类
        $cate=DB::name('category')->where('pid',0)->where('status',1)->order('displayorder desc')->select();
        foreach($cate as $key=>$value)
        {
            $cate_second=DB::name('category')->where('pid',$value['id'])->where('status',1)->order('displayorder desc')->select();
            $cate[$key]['cate_second']=$cate_second;
        }

        $cid=input('cid')?input('cid'):'';
        $title=input('title')?input('title'):'';
        $where="id>0";
        if($title){
            $where.=" and title like '%$title%'";
        }

        if($cid){
            $where.=" and cid = $cid ";
        }

        $articles=DB::name('article')->where($where)->order('displayorder desc')->select();
        foreach($articles as $key=>$value)
        {
            $articles[$key]['status']=$this->is_show($value['status']);
            $articles[$key]['cname']=$this->getcname($value['cid']);
            $articles[$key]['addtime']=date('m-d H:i',$value['addtime']);
            $articles[$key]['updatetime']=date('m-d H:i',$value['updatetime']);
        }
        $this->assign('cate',$cate);
        $this->assign('articles',$articles);
        $this->assign('title',$title);
        $this->assign('cid',$cid);
        return $this->fetch();
    }

    //添加/编辑article
    public function postdata()
    {
        if(!request()->isPost())
        {
            $id=input('id');
            if(isset($id)){
                $article_info=DB::name('article')->where('id',$id)->find();
                $this->assign('article_info',$article_info);
            }
            //文章分类
            $cate=DB::name('category')->where('pid',0)->where('status',1)->order('displayorder desc')->select();
            foreach($cate as $key=>$value)
            {
                $cate_second=DB::name('category')->where('pid',$value['id'])->where('status',1)->order('displayorder desc')->select();
                $cate[$key]['cate_second']=$cate_second;
            }
            $this->assign('cate',$cate);
            return $this->fetch();
        }
        else
        {
            $postdata = input('post.');
            $data['title'] = $postdata['title'];
            $data['cid'] = $postdata['cid'];
            isset($postdata['poster']) && $data['poster'] = $postdata['poster'];
            isset($postdata['videoposter']) && $data['videoposter'] = $postdata['videoposter'];
            isset($postdata['video']) && $data['video'] = $postdata['video'];
            isset($postdata['content']) && $data['content'] = $postdata['content'];
            $data['status'] = $postdata['status'];
            $data['displayorder'] = $postdata['displayorder'];
            $data['link'] = $postdata['link'];
            $data['views'] = $postdata['views'];
            $data['updatetime']=time();
            if($postdata['id']!=0)
            {
                DB::name('article')->where('id',$postdata['id'])->update($data);
            }
            else
            {
                $data['addtime']=time();
                DB::name('article')->insert($data);
            }
            $json = array();
            $json['status'] = '200';
            $json['data'] = $data;
            echo json_encode($json);
        }
    }

    //删除
    public function del()
    {
        $id=input('id');
        if(!$id)
        {
            $this->ajaxReturn('未检测到文章id');
        }
        $res=DB::name('article')->where('id',$id)->delete();
        if($res)
        {
            $this->ajaxJson('删除成功');
        }
    }
}
